$(function(){
	var userGrid = $("#backGrid");
	var isFinish={
			true:'<img src="public/easyui/themes/icons/ok.png">',
			false:'<img src="public/easyui/themes/icons/no.png">'
		}
	userGrid.datagrid({
		url: 'payback/list',
		fit:true,
		border:false,
		fitColumns:true,
		rownumbers:true,
		pagination : true,
		emptyMsg:"<img src='public/images/notk.png'>",
		columns : [ [ {
		
			field : 'student',
			title : '退款学员',
			align: 'center',
			width : 100,
			formatter: function (value, rec) {
		         return rec.student.name;
		     }
		}, {
			field : 'money',
			title : '退款金额',
			align: 'center',
			width : 100
		}, {
			
			field : 'backTime',
			align: 'center',
			title : '退款时间',
			width : 100
		}, {
			field : 'remark',
			align: 'center',
			title : '备注',
			width : 100
		}, {
			field : 'contract',
			title : '是否上交就业合同',
			align: 'center',
			width : 113,
			formatter : function(val){
				return isFinish[val];
			}
		}, {
			field : 'ac',
			title : '操作',
			width : 100,
			align : 'center',
			formatter:function(value,row,index){
				var bts = [];
				bts.push('<a data-index="'+index+'" class="ctr edit">编辑</a>');
				bts.push('<a data-id="'+row.id+'" data-index="'+index+'" class="ctr delete">删除</a>');
				return bts.join('');
			}
		} ] ],
		toolbar:[
		     {
		    	 iconCls:'icon-add',
		    	 text:"录入退款学员",
		    	 url:"add_payrecord.jsp",
		    	 handler : function() {
					backForm();
				}
		     }]
	});
	var student = $("#student");
	//检索按钮时间
	$("#paybackSearch").on("click",function(){
		userGrid.datagrid("load",{
			student:student.val(),
		
		});
	});
	var form = $("#paybackSearchForm");

	$("#paybackReset").on('click', function() {
		form.form('clear');
		// 清除查询参数
		userGrid.datagrid("load", {});
	});
	
	//操作
	var gridPanel = userGrid.datagrid("getPanel");
	gridPanel.on("click","a.edit",function(){
		var index = this.dataset.index;
		// 如果只有下标，没有id的情况，要获取id
		var rows = userGrid.datagrid("getRows");
		// 同下标获取对应行的数据对象
		var id = rows[index].id;
		
		backForm(id);
		
	}).on("click","a.delete",function(){
		var index = this.dataset.index;
		var id = this.dataset.id;
		$.messager.confirm("提示", "您确定要删除此数据吗？", function(r) {
			if(r) {
				$.get("payback/delete/" + id, function(rs) {
					if (rs.success) {
						// 刷新表格
						userGrid.datagrid("reload");
					} else {
						$.message.alert("提示", rs.message);
					}
				});
			}
		});
	});
	
	
	function backForm(id) {
		// 创建窗口
		var dialog = $("<div/>").dialog({
			href : "payback/add_payback"+ (id ? ("/" + id) : ""),
			title : "录入退款学员信息",
			height : 440,
			width : 500,
			modal : true,
			onClose : function() {
				$(this).dialog("destroy");
			},
			buttons : [ {
				iconCls : 'icon-save',
				text : '保存',
				handler : function() {
					var userForm = $("#backForm");
					if (userForm.form('validate')) {
						
						$.post("payback/save", userForm.serialize(), function(rs) {
							if (rs.success) {
								//刷新数据表格
								userGrid.datagrid("reload");
								//关闭窗口
								dialog.dialog("close");
							} else {
								$.messager.alert("提示", rs.message);
							}
						});
					}
				}
			} ,{
				iconCls : 'icon-cancel',
				text:'取消',
				handler:function(){
				dialog.dialog("close");
				}
			}]
			
			
		});
	}
});
	